package prictice.visits;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IRichBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Tuple;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Package prictice.visits
 * @Description:
 * @Author elwyn
 * @Date 2017/9/14 23:48
 * @Email elonyong@163.com
 */
public class PVSumBolt implements IRichBolt {
    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {

    }

    Map<Long, Long> counts = new HashMap<>();
    private String logString = null;
    private String sessionId = null;
    static long pv;

    @Override
    public void execute(Tuple tuple) {
        Long threadId = tuple.getLong(0);
        Long pv = tuple.getLong(1);
        counts.put(threadId, pv);
        long wordSum = 0;
        Iterator<Long> iterator = counts.values().iterator();
        while (iterator.hasNext()) {
            wordSum += iterator.next();
        }

        System.err.println(Thread.currentThread().getName() + " pv = " + wordSum);
    }

    @Override
    public void cleanup() {

    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {

    }

    @Override
    public Map<String, Object> getComponentConfiguration() {
        return null;
    }
}
